package service

import (
	"context"
	"github.com/gogf/gf/v2/util/gconv"
	"miaoyuadmin/internal/dao"
	"miaoyuadmin/internal/model"
	"miaoyuadmin/internal/model/entity"
	function "miaoyuadmin/utility"
)

type loginService struct {
}

var loginS = loginService{}

func Login() *loginService {
	return &loginS
}

func (s *loginService) CheckUserPassword(ctx context.Context, input model.UserLoginInput) (resUser entity.MyUser, resErr error) {
	//对密码进行rsa解密
	originData, rsaErr := function.RsaDecrypt([]byte(input.Password))
	if rsaErr != nil {
		resErr = rsaErr
		return
	}
	var shaWord string = function.SHA1String(gconv.String(originData))

	resUser, resErr = dao.MyUser.GetUserByUserNamePassword(ctx, input.UserName, shaWord)

	if resErr != nil {
		return
	}
	return
}

func (s *loginService) GetPayloadFormat(data entity.MyUser) (res map[string]interface{}) {
	payloadMap := make(map[string]interface{})
	payloadMap["id"] = data.Id
	payloadMap["username"] = data.UserName
	res = payloadMap
	return
}
